package com.umeng.soexample.mvp.view.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.umeng.lib_core.base.BaseFragment;
import com.umeng.soexample.R;
import com.umeng.soexample.bean.BrandEntity;
import com.umeng.soexample.bean.BrandListEntity;
import com.umeng.soexample.di.compont.DaggerBrandFragmentCompont;
import com.umeng.soexample.di.modules.BrandFragmentModules;
import com.umeng.soexample.listener.HomeBannerImageLoader;
import com.umeng.soexample.mvp.contract.BrandFragmentContract;
import com.umeng.soexample.mvp.view.adapter.BrandFragmentAdapter;
import com.umeng.soexample.mvp.view.adapter.BrandHotRecommendAdaper;
import com.umeng.soexample.mvp.view.adapter.BrandNewRecommendAdaper;
import com.umeng.soexample.mvp.view.adapter.BrandRecommendAdapter;
import com.umeng.soexample.mvp.persenter.BrandFragmentPresenter;
import com.umeng.soexample.mvp.view.adapter.BrandAdapter;
import com.umeng.soexample.mvp.view.custom.LetterView;
import com.umeng.soexample.network.API;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 品牌页面
 */
public class BrandFragment extends BaseFragment<BrandFragmentPresenter> implements BrandFragmentContract.BrandFragmentIView,  View.OnClickListener {


    private View view;
    private LinearLayout ll_brand_search;
    private RecyclerView rv_brand_fragment;
    private BrandFragmentAdapter brandFragmentAdapter;
    private LetterView lv_letters;
    private int xb = 0;
    //二级菜单列表集合
    private List<BrandListEntity>brandListEntities = new ArrayList<>();
    private BrandAdapter brandAdapter;
    private LinearLayoutManager bran_list_manager;
    //新入驻品牌集合
    private List<BrandEntity.Values> newBrandEntities = new ArrayList<>();
    private BrandNewRecommendAdaper newRecommendAdaper;
    //热门品牌集合
    private List<BrandEntity.Values> hotBrandEntities = new ArrayList<>();
    private BrandHotRecommendAdaper hotRecommendAdaper;
    //推荐品牌集合
    private List<BrandEntity.Values> recommendEntities = new ArrayList<>();
    private Banner banner;
    private RecyclerView rv_recommend;
    private RadioGroup rg_item_brand_recommend;
    private BrandRecommendAdapter recommendAdapter;
    //广告集合
    private List<BrandEntity.Values> bannerEntities = new ArrayList<>();
    private View recommend;


    @Override
    public View createView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_brand, null);
        return view;
    }

    @Override
    public void init(View view) {
        lv_letters = view.findViewById(R.id.lv_letters);
        rv_brand_fragment = view.findViewById(R.id.rv_brand_fragment);
        rv_brand_fragment.setLayoutManager(new LinearLayoutManager(getActivity()));
        brandFragmentAdapter = new BrandFragmentAdapter(R.layout.item_category_left);
        //初始化推荐控件
        recommend = View.inflate(getActivity(), R.layout.item_brand_recommend, null);
        ll_brand_search = recommend.findViewById(R.id.ll_brand_search);
        banner = recommend.findViewById(R.id.ban_item_brand_recommend);
        rv_recommend = recommend.findViewById(R.id.rv_item_brand_recommend);
        rg_item_brand_recommend = recommend.findViewById(R.id.rg_item_brand_recommend);
        //recommend横向滑动广告推荐配置
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv_recommend.setLayoutManager(layoutManager);
        recommendAdapter = new BrandRecommendAdapter(R.layout.item_brand_recommend_item);
        rv_recommend.setAdapter(recommendAdapter);
        //添加头布局
        bran_list_manager = new LinearLayoutManager(getActivity());
        rv_brand_fragment.setAdapter(brandFragmentAdapter);
        //初始化全部品牌适配器
        brandAdapter = new BrandAdapter(brandListEntities);
        //初始化新入驻品牌适配器
        newRecommendAdaper = new BrandNewRecommendAdaper(R.layout.item_brand_new_recommend);
        //初始化热门品牌适配器
        hotRecommendAdaper = new BrandHotRecommendAdaper(R.layout.item_brand_new_recommend);
    }

    @Override
    public void initData() {
        p.requestBrand("1");
    }

    @Override
    public void setupDagger() {
        DaggerBrandFragmentCompont.builder()
                .brandFragmentModules(new BrandFragmentModules(this)).build().inject(this);
    }

    @Override
    public void initListener() {
        //品牌按钮组点击事件
        rg_item_brand_recommend.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_item_brand_recommend_all:
                        newRecommendAdaper.removeHeaderView(recommend);
                        hotRecommendAdaper.removeHeaderView(recommend);
                        brandAdapter.removeHeaderView(recommend);
                        brandAdapter.addHeaderView(recommend, 0);
                        lv_letters.setVisibility(View.VISIBLE);
                        rv_brand_fragment.setLayoutManager(bran_list_manager);
                        rv_brand_fragment.setAdapter(brandAdapter);
                        break;
                    case R.id.rb_item_brand_recommend_new:
                        lv_letters.setVisibility(View.GONE);
                        brandAdapter.removeHeaderView(recommend);
                        hotRecommendAdaper.removeHeaderView(recommend);
                        newRecommendAdaper.removeHeaderView(recommend);
                        newRecommendAdaper.addHeaderView(recommend, 0);
                        rv_brand_fragment.setLayoutManager(new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false));
                        rv_brand_fragment.setAdapter(newRecommendAdaper);
                        break;
                    case R.id.rb_item_brand_recommend_hot:
                        lv_letters.setVisibility(View.GONE);
                        brandAdapter.removeHeaderView(recommend);
                        newRecommendAdaper.removeHeaderView(recommend);
                        hotRecommendAdaper.removeHeaderView(recommend);
                        hotRecommendAdaper.addHeaderView(recommend, 0);
                        rv_brand_fragment.setLayoutManager(new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false));
                        rv_brand_fragment.setAdapter(hotRecommendAdaper);
                        break;
                }
            }
        });
        rg_item_brand_recommend.check(R.id.rb_item_brand_recommend_all);

        lv_letters.setListener(letterListener);
        ll_brand_search.setOnClickListener(this);


        //推荐品牌点击事件
//        recommendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//
//                Intent goodsValues = new Intent(getActivity(), FragmentActivity.class);
//                goodsValues.putExtra("type", "goods_values");
//                goodsValues.putExtra("goods_id", recommendAdapter.getData().get(position).brand_id);
//                getActivity().startActivity(goodsValues);
//            }
//        });

//        //全部品牌点击事件
//        brandAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                if (((BrandAdapter)adapter).getData().get(position).getItemType() == 2) {
//                    Toast.makeText(getActivity(), ((BrandAdapter) adapter).getData().get(position).brand_name, Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
//        //新入驻品牌点击事件
//        newRecommendAdaper.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(getActivity(), ((BrandNewRecommendAdaper)adapter).getData().get(position).brand_name, Toast.LENGTH_SHORT).show();
//            }
//        });
//        //热门品牌点击事件
//        hotRecommendAdaper.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(getActivity(), ((BrandHotRecommendAdaper)adapter).getData().get(position).brand_name, Toast.LENGTH_SHORT).show();
//            }
//        });
//        //推荐品牌点击事件
//        recommendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(getActivity(), ((BrandRecommendAdapter)adapter).getData().get(position).brand_name, Toast.LENGTH_SHORT).show();
//            }
//        });
//        //banner点击事件
//        banner.setOnBannerClickListener(new OnBannerClickListener() {
//            @Override
//            public void OnBannerClick(int position) {
//                Toast.makeText(getActivity(), bannerEntities.get(position).brand_name, Toast.LENGTH_SHORT).show();
//            }
//        });

    }

    @Override
    public void brandSuccess(Map<String, List> map) {
        //品牌列表集合赋值并刷新适配器
        brandListEntities.addAll(map.get("brandListEntities"));
        brandAdapter.setNewData(brandListEntities);
        //品牌推荐集合赋值并刷新适配器
        recommendEntities.addAll(map.get("recommendEntities"));
        recommendAdapter.setNewData(recommendEntities);
        //品牌广告集合赋值并刷新适配器
        bannerEntities.addAll(map.get("bannerEntities"));
        initBanner(bannerEntities);
        newBrandEntities.addAll(map.get("newBrandEntities"));
        newRecommendAdaper.setNewData(newBrandEntities);
        hotBrandEntities.addAll(map.get("hotBrandEntities"));
        hotRecommendAdaper.setNewData(hotBrandEntities);
    }

    private void initBanner(List<BrandEntity.Values> bannerEntities){

        List<String> strings = new ArrayList<>();
        for (BrandEntity.Values values : bannerEntities){
            strings.add(API.BaseUrl + values.brand_icon);
        }
        banner.setImages(strings);
        banner.setImageLoader(new HomeBannerImageLoader());
        banner.isAutoPlay(true);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setDelayTime(1 * 1500);
        banner.start();
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }



    /**
     * 右侧首字母监听
     * @param letter
     */
    private LetterView.SelectLetterListener letterListener = new LetterView.SelectLetterListener() {
        @Override
        public void selectLetter(String letter) {
            for (int i = 0; i < brandListEntities.size(); i++){
                if (letter.equals(brandListEntities.get(i).brand_letter)){
                    Log.e("tt", "selectLetter: letter -> " + letter + "、brandListEntities.get(i).brand_letter -> " + brandListEntities.get(i).brand_letter );
                    xb = i;
                    Log.e("tt", "selectLetter: xb -> " + xb );
                    break;
                }
            }
            rv_brand_fragment.scrollToPosition(xb + 1);
            LinearLayoutManager layoutManager = (LinearLayoutManager) rv_brand_fragment.getLayoutManager();
            layoutManager.scrollToPositionWithOffset(xb + 1, 0);
            rv_brand_fragment.setLayoutManager(layoutManager);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_brand_search:
                Toast.makeText(getActivity(), "搜索框", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
